package com.unam.agenda;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ListaContactos extends Activity {
	
	DatabaseAdapter db;
	ListView list;
	ArrayList<String> contactos=null;
	Button todo;
	
	protected static final int CONTEXTMENU_DELETEITEM=0;
	protected static final int CONTEXTMENU_UPDATEITEM=1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listacontactos);
		
		list=(ListView) findViewById(R.id.listView1);
		todo=(Button) findViewById(R.id.btneliminar);
		
		db=new DatabaseAdapter(this);
		db.open();
		
		if(db!=null){
			
			contactos=getContacts();
			final  ArrayAdapter<String> adList=
					new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactos);
			list.setAdapter(adList);
			list.setCacheColorHint(0);
		}
		else{
			Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
		}
		db.close();
		
		list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				// TODO Auto-generated method stub
				menu.setHeaderTitle("ELija una opcion");
				menu.add(0,CONTEXTMENU_UPDATEITEM,0,"Actualizar");
				menu.add(0,CONTEXTMENU_DELETEITEM,0,"Eliminar");
				
			}
		});

	}
	private ArrayList<String> getContacts(){
		
		Cursor c=db.todosContactos();
		ArrayList<String> contact=new ArrayList<String>();
		
		if(c.moveToFirst()){
			
			do{
				int id=c.getInt(0);
				contact.add(c.getString(1));
				String telefono=c.getString(2);
				Log.w("Contacto", "Telefono: "+telefono+"Id:"+id);
			}while(c.moveToNext());
		}
		return contact;
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		AdapterContextMenuInfo menuInfo=(AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case CONTEXTMENU_DELETEITEM:
			
			db.open();
			String elimina=list.getAdapter().getItem(menuInfo.position).toString();
			long id=db.getContact(elimina);
			Log.i("ID", "Id: "+id+" nombre: "+elimina);
			contactos.remove(elimina);
			
			if(db.deleteAgenda(id))
				Log.i("DB", elimina+" eliminando");
			else
				Log.e("BD", "Error al eliminar "+elimina);
			
			ArrayAdapter<String> adList=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactos);
			list.setAdapter(adList);
			db.close();
			return true;
			
		case CONTEXTMENU_UPDATEITEM:
			db.open();
			return true;
		}
		return false;
		
	
	}

}
